home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / lk_df.zip / DF.C < prev    next >
C/C++ Source or Header  |  1993-02-23  |  10KB  |  277 lines

  1. //===========================================================================
  2. //
  3. //      df.c - display free space on mounted disks
  4. //
  5. //      Author:  Larry Kollar <larryk@cton.com>
  6. //               Basically a slight modification to drives.c by Jon Frievald
  7. //
  8. //
  9. //===========================================================================
  10.  
  11. #pragma inline                  // start compilation via assembly to eliminate
  12.                                 // the error message generated
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <dos.h>
  16. #include <dir.h>
  17. #include "dfbits.h"
  18.  
  19. //===========================================================================
  20. //
  21. //      function declarations
  22. //
  23. //===========================================================================
  24.  
  25. void    main ( int, char ** );
  26. int     check_disk_type ( int disk_number );
  27. int     test_cd_driver ( void );
  28. char *  get_label( int disk_number );
  29. void    print_disk_info( int disk_number, int disk_type );
  30. extern  void setCEH ( void );
  31. extern  void unsetCEH ( void );
  32. extern  unsigned short  number_floppy ( void );
  33. extern  int     found_an_option( int, char ** );
  34. extern  void check_drive_args( int, char **, long * );
  35.  
  36. //===========================================================================
  37. //
  38. //      character arrays
  39. //
  40. //===========================================================================
  41.  
  42. char    *drive_type     [] = { "", "160K diskette", "180K diskette",\
  43.         "320K diskette", "360K diskette", "1.2M diskette", "720K diskette",\
  44.     "1.44M diskette", "unknown floppy", "fixed disk",\
  45.     "network file service", 0 };
  46.  
  47. //===========================================================================
  48. //
  49. //      miscellany
  50. //
  51. //===========================================================================
  52.  
  53. // #define SMALL   0
  54. // char    memory_model = SMALL;   // needed for numflopy.asm
  55.  
  56. //===========================================================================
  57. //
  58. //      main()
  59. //
  60. //              arguments:      currently none
  61. //
  62. //              returns:        0
  63. //
  64. //===========================================================================
  65.  
  66. void main ( int argc, char **argv )
  67.  
  68. {
  69.     int  i, disk_type_found;
  70.     static long drive_bits;
  71.  
  72.  
  73.     SET_ALL( drive_bits );
  74.     if( found_an_option(argc, argv) )
  75.         exit( EXIT_FAILURE );
  76.  
  77.     check_drive_args( argc, argv, &drive_bits );
  78.  
  79.     setCEH();
  80.  
  81.     printf( "%s%s\n", "Drive  Label          Type                 ",
  82.             "  K total   K used   K free" );
  83.  
  84.     /* check the floppies:  i=0 means Drive A:, etc. */
  85.  
  86.     for ( i=0; i < number_floppy(); i++ )
  87.     {
  88.         if( GET_BIT(drive_bits, i) )  /* do we want to look for it? */
  89.         {
  90.             disk_type_found = check_disk_type ( i+1 );
  91.             if ( disk_type_found != 0 )
  92.                 print_disk_info( i+1, disk_type_found );
  93.         }
  94.     }
  95.  
  96.     if ( i == 1 )
  97.     i++;
  98.  
  99.     for ( ; i < 27; i++ )
  100.     {
  101.         if( GET_BIT(drive_bits, i) )  /* do we want to bother looking? */
  102.         {
  103.             disk_type_found = check_disk_type ( i+1 );
  104.             if ( disk_type_found != 0 )
  105.                 print_disk_info( i+1, disk_type_found );
  106.         }
  107.     }
  108.  
  109.     printf("\n");
  110.  
  111.     unsetCEH();
  112.  
  113.     exit( EXIT_SUCCESS );
  114. }
  115.  
  116. //===========================================================================
  117. //
  118. //      check_disk_type()
  119. //
  120. //              determines whether disk is local, remote, or CD-ROM
  121. //
  122. //              arguments:      int     disk_number     number of disk as
  123. //                                                      from getdisk()
  124. //
  125. //              returns:        int     disk type;
  126. //                                                      0 = no disk
  127. //                                                      1 = 160K floppy
  128. //                                                      2 = 180K floppy
  129. //                                                      3 = 320K floppy
  130. //                                                      4 = 360K floppy
  131. //                                                      5 = 1.2M floppy
  132. //                                                      6 = 720K floppy
  133. //                                                      7 = 1.44M floppy
  134. //                                                      8 = unknown floppy
  135. //                                                      9 = local
  136. //                                                      10 = network
  137. //                                                      11 = CD-ROM
  138. //
  139. //        uses global:    cd_driver       (0 if no driver or < 2.0, 1 if >= 2.0)
  140. //
  141. //===========================================================================
  142.  
  143. int check_disk_type ( int disk_number )
  144.  
  145. {
  146.         union   REGS    regs;
  147.         struct  SREGS   sregs;
  148.         unsigned char far *des_byte;            // media descriptor byte
  149.         int     rc = 1;
  150.  
  151.         regs.h.dl = disk_number;                // drive number to test (a: == 1)
  152.         regs.h.ah = 0x1C;                       // "get drive data" function
  153.         int86x ( 0x21, ®s, ®s, &sregs );
  154.  
  155.         if ( regs.h.al == 0xFF )                // Did an error occur?
  156.                 {
  157.                 return ( 0 );                   // Assume no disk present
  158.                 }
  159.         des_byte = MK_FP ( sregs.ds, regs.x.bx );       // Make the far pointer to the descriptor byte
  160.  
  161.         if ( ( des_byte[0] != 0xF8 ) && ( des_byte[0] != 0x00 ) )       // Is it a fixed disk?
  162.  
  163.         /*  This is where the code varies from the documentation I can find on
  164.             the subject -- it *SEEMS* that CD's return a descriptor byte of
  165.             0x00, however, I cannot find that documented anywhere... (0xF8 is
  166.             fixed disk).  I've had to include the test for 0x00 here so that we
  167.             progress to the tests further down the line... */
  168.  
  169.                 {
  170.                 regs.h.ah = 0x36;               // If not, get the number of sectors on it
  171.                 regs.h.dl = disk_number;        //   to determine the type of floppy it is
  172.                 int86 ( 0x21, ®s, ®s );   //   (the descriptor byte is not specific enough...)
  173.                 switch ( regs.x.dx * regs.x.ax )
  174.                         {
  175.                         case 313:               // 160K
  176.                                 return ( 1 );
  177.                         case 351:               // 180K
  178.                                 return ( 2 );
  179.                         case 630:               // 320K
  180.                                 return ( 3 );
  181.                         case 708:               // 360K
  182.                                 return ( 4 );
  183.                         case 2371:              // 1.2M
  184.                                 return ( 5 );
  185.                         case 1426:              // 720K
  186.                                 return ( 6 );
  187.                         case 2847:              // 1.44M
  188.                                 return ( 7 );
  189.                         default:                // Unknown type
  190.                                 return ( 9 );
  191.                         }
  192.                 }
  193.  
  194.         // If we get this far, it's a hard, net or CD-ROM disk
  195.  
  196.         rc = 9;                    // Set rc to 9 if we get this far because
  197.                                    //   it's at least a local hard disk
  198.         regs.h.ah = 0x44;          // Int 21, func 44, sub 9 is IOCTL, we
  199.         regs.h.al = 0x9;           //   use it to diff local & net/cd (remote)
  200.                                    //   This requires DOS 3.1 or higher
  201.         regs.h.bl = (unsigned char) disk_number;
  202.         int86 ( 0x21, ®s, ®s );
  203.  
  204.         if ( ( regs.x.dx & 0x1000 ) == 0x1000 ) // if bit 12 of DX is 1, disk is remote
  205.         {
  206.             rc = 10;                    // if we're here, we're either net or cd
  207.         }
  208.         return ( rc );
  209. }
  210.  
  211. // =============================================================================
  212. // The rest of this file is from Larry Kollar, and is public domain (but if you
  213. // use this code in your own projects, remember I are not a programmer).
  214. // =================================================